<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat Interface with History and Conversation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: auto;
            padding: 10px;
        }
        #chatBox {
            border: 1px solid #ccc;
            padding: 10px;
            height: 400px;
            overflow-y: scroll;
            margin-bottom: 10px;
        }
        .message {
            padding: 5px;
            border-radius: 5px;
            margin-bottom: 5px;
            white-space: pre-wrap;
        }
        .user {
            background-color: #e0f7fa;
            text-align: right;
        }
        .assistant {
            background-color: #e8f5e9;
            text-align: left;
        }
        #userInput {
            width: 100%;
            padding: 10px;
            font-size: 16px;
        }
    </style>
</head>
<body>

<h1>Chat Interface with History and Conversation</h1>

<div id="chatBox"></div>
<input type="text" id="userInput" placeholder="Type a message..." onkeydown="if(event.key === 'Enter') sendMessage()">

<script>
    // Fetch and display chat history
    async function fetchHistory() {
        try {
            const response = await fetch('http://localhost:8088/ai/history');
            if (!response.ok) throw new Error('Network response was not ok');

            const history = await response.json();
            history.forEach(item => {
                const role = item.role === 1 ? 'user' : 'assistant';
                displayMessage(role, item.content);
            });
        } catch (error) {
            console.error('Error fetching chat history:', error);
        }
    }

    // Display message in the chat box
    function displayMessage(role, content) {
        const chatBox = document.getElementById('chatBox');
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${role}`;
        messageDiv.innerText = content;
        chatBox.appendChild(messageDiv);
        chatBox.scrollTop = chatBox.scrollHeight;
    }

    // Send a message and display assistant's response
    async function sendMessage() {
        const userInput = document.getElementById('userInput');
        const messageContent = userInput.value.trim();

        if (!messageContent) return;

        // Display user's message
        displayMessage('user', messageContent);
        userInput.value = '';

        try {
            const response = await fetch(`http://localhost:8088/ai/example?message=${encodeURIComponent(messageContent)}`);
            if (!response.ok) throw new Error('Network response was not ok');

            const assistantResponse = await response.text();
            displayMessage('assistant', assistantResponse);
        } catch (error) {
            console.error('Error fetching assistant response:', error);
            displayMessage('assistant', 'Error: Unable to get response');
        }
    }

    // Load chat history when the page loads
    window.onload = fetchHistory;
</script>

</body>
</html>
